package com.csun.cmny.provider.controller;


import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.LongRangeServiceOrderStatusConstant;
import com.csun.cmny.provider.model.domain.OmcLongRangeServiceOrder;
import com.csun.cmny.provider.model.domain.OmcSpecialist;
import com.csun.cmny.provider.model.domain.UacStaff;
import com.csun.cmny.provider.model.dto.specialist.SpecialistQueryDto;
import com.csun.cmny.provider.model.dto.specialist.SpecialistSaveDto;
import com.csun.cmny.provider.model.dto.specialist.SpecialistUpdateDto;
import com.csun.cmny.provider.model.vo.specialist.SpecialistQueryVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcLongRangeServiceOrderService;
import com.csun.cmny.provider.service.OmcSpecialistService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/specialist", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcSpecialistController extends BaseController {
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcSpecialistService specialistService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private UacRpcService rpcService;

    @Resource
    private OmcLongRangeServiceOrderService longRangeServiceOrderService;


    @ValidateAnnotation
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addChanner(@RequestBody @Valid SpecialistSaveDto specialistSaveDto, BindingResult br) {

        //判断服务人员是否存在
        Integer isStaff=rpcService.isStaff(specialistSaveDto.getStaffId());

        if (isStaff<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("specialist.staff.isnull", null));
        }

        //校验此服务人员是否已经被添加成专家
        OmcSpecialist specialist = new OmcSpecialist();
        specialist.setStaffId(specialistSaveDto.getStaffId());
        List<OmcSpecialist> select = specialistService.select(specialist);
        if (PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("specialist.staff.error", null));
        }

        OmcSpecialist omcSpecialist = new OmcSpecialist();

        BeanUtils.copyProperties(specialistSaveDto,omcSpecialist);

        omcSpecialist.setGroupId(getLoginAuthDto().getGroupId());
        omcSpecialist.setUserId(getLoginAuthDto().getUserId());
        omcSpecialist.setOperator(getLoginAuthDto().getUserName());


        int save = specialistService.save(omcSpecialist);



        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),save);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改专家")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper update(@RequestBody @Valid SpecialistUpdateDto specialistUpdateDto, BindingResult br) {

        OmcSpecialist omcSpecialist = new OmcSpecialist();

        BeanUtils.copyProperties(specialistUpdateDto,omcSpecialist);

        int update = specialistService.update(omcSpecialist);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "分页查询专家")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper select(@RequestBody SpecialistQueryDto specialistQueryDto) {

        List<SpecialistQueryVo>list=specialistService.selectSpecialist(specialistQueryDto);

        for (SpecialistQueryVo specialistQueryVo : list) {
            if(specialistQueryVo.getIsPutaway()==0){
                specialistQueryVo.setIsPutaways(true);
            }
        }

        PageInfo<SpecialistQueryVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "删除专家")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper delete(Long id) {

        //该医生名下是否有未完成订单
        OmcLongRangeServiceOrder longRangeServiceOrder = new OmcLongRangeServiceOrder();
        longRangeServiceOrder.setSpecialistId(id);

        List<OmcLongRangeServiceOrder> select = longRangeServiceOrderService.select(longRangeServiceOrder);

        for (OmcLongRangeServiceOrder omcLongRangeServiceOrder : select) {
            Integer status = omcLongRangeServiceOrder.getOrderStatus();
            if (status==LongRangeServiceOrderStatusConstant.PLACEDANORDER||status==LongRangeServiceOrderStatusConstant.WAITFORREPLY||status==LongRangeServiceOrderStatusConstant.TOBECONFIRMED||status==LongRangeServiceOrderStatusConstant.ALREADYIDENTIFIED){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("specillist.delete.error", null));
            }
        }


        int delete = specialistService.deleteByKey(id);



        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),delete);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "专家上线")
    @RequestMapping(value = "/on", method = RequestMethod.POST)
    public Wrapper on(Long id) {

        OmcSpecialist omcSpecialist = new OmcSpecialist();

        omcSpecialist.setIsPutaway(0);
        omcSpecialist.setId(id);
        int update = specialistService.update(omcSpecialist);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "专家下线")
    @RequestMapping(value = "/off", method = RequestMethod.POST)
    public Wrapper off(Long id) {

        OmcSpecialist omcSpecialist = new OmcSpecialist();

        omcSpecialist.setIsPutaway(1);
        omcSpecialist.setId(id);
        int update = specialistService.update(omcSpecialist);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }




}
